package com.example.batch.config;

import com.example.batch.constant.JobConstant;
import com.example.batch.task.MyTasklet;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/7/17 16:50
 * @Description TODO
 */
@Configuration
public class SpiltConfig extends BaseConfig {

    /**
     * 创建三个step
     *
     * @return
     */
    @Bean
    public Step splitDemo1() {
        return stepBuilderFactory.get("splitDemo1")
                .tasklet(new MyTasklet("splitDemo1")).build();
    }

    @Bean
    public Step splitDemo2() {
        return stepBuilderFactory.get("splitDemo2")
                .tasklet(new MyTasklet("splitDemo2")).build();
    }

    @Bean
    public Step splitDemo3() {
        return stepBuilderFactory.get("splitDemo3")
                .tasklet(new MyTasklet("splitDemo3")).build();
    }

    @Bean
    public Step splitDemo4() {
        return stepBuilderFactory.get("splitDemo4")
                .tasklet(new MyTasklet("splitDemo4")).build();
    }


    @Bean
    public Flow splitFlowDemo() {
        return new FlowBuilder<Flow>("splitFlowDemo")
                .split(taskExecutor)
                .add(splitFlowDemo1(),splitFlowDemo2())
                .build();
    }

    @Bean
    public Flow splitFlowDemo1() {
        return new FlowBuilder<Flow>("splitFlowDemo2")
                .start(splitDemo1())
                .next(splitDemo3())
                .build();
    }

    @Bean
    public Flow splitFlowDemo2() {
        return new FlowBuilder<Flow>("splitFlowDemo2")
                .start(splitDemo2())
                .build();
    }


    @Bean
    public Job splitDemojob() {
        return jobBuilderFactory
                .get(JobConstant.SPILT_JOB_DEMO)
                .start(splitFlowDemo())
                .next(splitDemo4())
                .end().build();

    }


}
